﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-20 17:58:33
// 负责人：liucx
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
using OF.Component.DTO;

namespace OF.Component.DAL
{

    /// <summary>
    /// 数据访问层
    /// </summary>	
    internal partial class Hospital
    {

        /// <summary>
        /// 根据断言条件获取单个
        /// </summary>
        public HospitalInfo QuerySingle(Expression<Func<HospitalInfo, bool>> predicate)
        {
            return SQLServerProvider.CreateObject(ConnectionStringKey, predicate);
        }
        /// <summary>
    	/// 根据断言条件获获取列表
    	/// </summary>
	    public IList<HospitalInfo> QueryList(Expression<Func<HospitalInfo, bool>> predicate)
        {
            return SQLServerProvider.CreateList(ConnectionStringKey, predicate);
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public IList<HospitalInfo> SearchList(HospitalCondition condition)
        {
            IList<HospitalInfo> list = new List<HospitalInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("[p_hospital_search]");
            //设置参数
            db.AddInParameter(dbc, "@Name", DbType.String, condition.Name);
            db.AddInParameter(dbc, "@CityID", DbType.Int32, condition.CityID);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, condition.page);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, condition.rows);
            db.AddOutParameter(dbc, "@OutCount", DbType.Int32, 4);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    list.Add(SQLServerProvider.Create<HospitalInfo>(dr));
                }
            }
            condition.total = (int)db.GetParameterValue(dbc, "@OutCount");
            return list;
        }

        /// <summary>
        /// 根据城市获取体检医院
        /// </summary>
        /// <param name="cityId"></param>
        /// <returns></returns>
        public List<HospitalInfo> GetByCity(int cityId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [Hospital] ");
            strSql.Append("WHERE [CityID]=@CityID ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@CityID", DbType.Int32, cityId);

            List<HospitalInfo> list = new List<HospitalInfo>();
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    HospitalInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            return list;
        }
    }

}